package com.vimi8.ebb.auth.service;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Created by wangyang on 2016/12/23.
 */
@Mapper
public interface RelationService {
    /**
     * 查询农场粉丝数
     * @param clientId
     * @return
     */
    @Select("select IFNULL(count(1),0) from user_org_relation where client_id=#{clientId} and relation_type=1")
    public int getFarmFansCount(@Param("clientId") String clientId);

    /**
     * 查询农场员工数
     * @param clientId
     * @return
     */
    @Select("select IFNULL(count(1),0) from user_org_relation where client_id=#{clientId} and relation_type=2 and `status`=1")
    public int getFarmEmpCount(@Param("clientId") String clientId);

    /**
     * 查询该用户与该农场是否关注
     * @param clientId
     * @param userId
     * @return
     */
    @Select("select IFNULL(count(1),0) from user_org_relation where client_id=#{clientId} and user_id=#{userId} and relation_type=1")
    public int getUserFarmFans(@Param("clientId") String clientId, @Param("userId") String userId);

    /**
     * 查询该用户与该农场是否加入
     * @param clientId
     * @param userId
     * @return
     */
    @Select("select COALESCE(`status`,0) as farmJoin from user_org_relation where client_id=#{clientId} and user_id=#{userId} and relation_type=2")
    public Integer getUserFarmJoin(@Param("clientId") String clientId, @Param("userId") String userId);

    /**
     * 分页获取用户关注的农场
     * @param userId
     * @return
     */
    @Select("select client_id from user_org_relation where user_id=#{userId} and relation_type=1" +
            " limit #{start},#{size}")
    public List<String> getUserConcernFarm(@Param("userId") String userId, @Param("start") int start, @Param("size") int size);

    @Select("select count(client_id) from user_org_relation where user_id=#{userId} and relation_type=1")
    public int getUserCocernFarmCount(@Param("userId") String userId);
    /**
     * 分页获取用户加入的农场
     * @param userId
     * @return
     */
    @Select("select client_id from user_org_relation where user_id=#{userId} and relation_type=2 " +
            " limit #{start},#{size}")
    public List<String> getUserJoinFarm(@Param("userId") String userId, @Param("start") int start, @Param("size") int size);

    @Select("select count(client_id) from user_org_relation where user_id=#{userId} and relation_type=2")
    public int getUserJoinFarmCount(@Param("userId") String userId);

    @Insert("insert into user_org_relation (id,user_id,client_id,relation_type) values(#{id},#{userId},#{clientId},#{relationType})")
    public int insertRelation(@Param("id") String id, @Param("userId") String userId, @Param("clientId") String clientId, @Param("relationType") String relationType);

    /**
     * 取消关注/取消加入
     *
     * @param userId
     * @param clientId
     * @param relationType 1：取消关注；2：取消加入
     * @return
     */
    @Insert("DELETE FROM user_org_relation WHERE user_id=#{userId} AND client_id=#{clientId} AND relation_type=#{relationType}")
    public int deleteRelation(@Param("userId") String userId, @Param("clientId") String clientId, @Param("relationType") String relationType);

}
